/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.*;
import entities.*;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author lion
 */
public class StudentDao {

    private Connection conn = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;
    MysqlHelper mysql = new MysqlHelper();

    public int getStudentId(Member member) throws Exception {
        int id = 0;
        try {
            conn = mysql.GetConnection();
            ps = conn.prepareStatement("SELECT * FROM fl_student WHERE memberid=?");
            ps.setInt(1, new MemberDao().getMemberIdByUsername(member));
            rs = ps.executeQuery();
            while (rs.next()) {
                id = rs.getInt("studentid");
            }
        } catch (Exception e) {
            throw e;
        }
        return id;
    }

    public int getMemberIdOfStudent(Student student) throws Exception {
        int id = 0;
        try {
            conn = mysql.GetConnection();
            ps = conn.prepareStatement("SELECT * FROM fl_student WHERE studentid=?");
            ps.setInt(1, student.getStudentid());
            rs = ps.executeQuery();
            while (rs.next()) {
                id = rs.getInt(student.getStudentid());
            }
        } catch (Exception e) {
            throw e;
        }
        return id;
    }

    public List<Student> getListStudent() throws Exception {
        List<Student> students = new ArrayList<Student>();
        conn = mysql.GetConnection();
        rs = mysql.ExecuteReader("SELECT *  FROM fl_student");
        while (rs.next()) {
            Student student = new Student();
            student.setStudentid(rs.getInt("studentid"));
            student.setCenterid(rs.getInt("centerid"));
            student.setMemberid(rs.getInt("memberid"));
            student.setCourseid(rs.getInt("courseid"));
            student.setName(rs.getString("name"));
            student.setAddress(rs.getString("address"));
            students.add(student);
        }
        return students;
    }
//     public void editCourse(Course course) throws Exception {
//        try {
//            conn = mysql.GetConnection();
//            mysql.ExecuteUpdate("UPDATE fl_course SET course_name='" + course.getCourseName()
//                    + "' WHERE courseid=" + course.getCourseid());
//            mysql.ExecuteUpdate("UPDATE fl_course SET Fee=" + course.getFee()
//                    + " WHERE courseid=" + course.getCourseid());
//            mysql.ExecuteUpdate("UPDATE fl_course SET Prerequisite='" + course.getPrerequisite()
//                    + "' WHERE courseid=" + course.getCourseid());
//        } catch (Exception e) {
//            throw e;
//        }
//    }
}
